{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "S={1, 2, 3, 4, 5, 6}:抛掷骰子的样本空间\n",
      "A1={2, 4, 6}:偶数点\n",
      "A2={1, 2, 3}:点数不超过3\n",
      "A1∪A2={1, 2, 3, 4, 6}:偶数点或点数不超过3\n",
      "A1∩A2={2}:不超过3的偶数点\n",
      "A1-A2={4, 6}:超过3的偶数点\n",
      "Ā1={1, 3, 5}:奇数点\n",
      "(A1∪A2)_=Ā1∩Ā2 is True\n",
      "(A1∩A2)_=Ā1∪Ā2 is True\n"
     ]
    }
   ],
   "source": [
    "#例1-7\n",
    "S=set({1, 2, 3, 4, 5, 6})\n",
    "A1=set([2, 4, 6])\n",
    "A2=set([1, 2, 3])\n",
    "print('S=%s:抛掷骰子的样本空间'%S)\n",
    "print('A1=%s:偶数点'%A1)\n",
    "print('A2=%s:点数不超过3'%A2)\n",
    "print('A1∪A2=%s:偶数点或点数不超过3'%(A1|A2))\n",
    "print('A1∩A2=%s:不超过3的偶数点'%(A1&A2))\n",
    "print('A1-A2=%s:超过3的偶数点'%(A1-A2))\n",
    "print('Ā1=%s:奇数点'%(S-A1))\n",
    "print('(A1∪A2)_=Ā1∩Ā2 is %s'%(S-(A1|A2)==(S-A1)&(S-A2)))\n",
    "print('(A1∩A2)_=Ā1∪Ā2 is %s'%(S-(A1&A2)==(S-A1)|(S-A2)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1)至少击中1枪={(1, 1, 0), (0, 1, 1), (0, 1, 0), (1, 0, 0), (0, 0, 1), (1, 0, 1), (1, 1, 1)}\n",
      "(2)3枪都击中={(1, 1, 1)}\n",
      "(3)只有第1枪击中目标={(1, 0, 0)}\n",
      "(4)恰击中1枪={(1, 0, 0), (0, 1, 0), (0, 0, 1)}\n",
      "(5)3枪都未击中={(0, 0, 0)}\n",
      "(6)至少有1枪未击中={(0, 1, 1), (1, 1, 0), (1, 0, 0), (0, 0, 1), (1, 0, 1), (0, 0, 0), (0, 1, 0)}\n"
     ]
    }
   ],
   "source": [
    "#例1-8\n",
    "from utility import subSet, permutations\n",
    "S=set(permutations([0, 1], 3, True))\n",
    "A1=subSet(S, lambda a: a[0]==1)\n",
    "A2=subSet(S, lambda a: a[1]==1)\n",
    "A3=subSet(S, lambda a: a[2]==1)\n",
    "A_1=S-A1\n",
    "A_2=S-A2\n",
    "A_3=S-A3\n",
    "print('(1)至少击中1枪=%s'%(A1|A2|A3))\n",
    "print('(2)3枪都击中=%s'%(A1&A2&A3))\n",
    "print('(3)只有第1枪击中目标=%s'%(A1&A_2&A_3))\n",
    "print('(4)恰击中1枪=%s'%(A1&A_2&A_3|A_1&A2&A_3|A_1&A_2&A3))\n",
    "print('(5)3枪都未击中=%s'%(S-(A1|A2|A3)))\n",
    "print('(6)至少有1枪未击中=%s'%(S-(A1&A2&A3)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A1∪A2={(1, 1, 0), (0, 0, 0), (1, 0, 0), (1, 1, 1), (1, 0, 1)}\n",
      "A1∩A2={(1, 1, 1)}\n",
      "A2-A1={(0, 0, 0)}\n",
      "A1∪Ā2={(1, 1, 0), (0, 1, 1), (0, 1, 0), (1, 0, 0), (0, 0, 1), (1, 0, 1), (1, 1, 1)}\n",
      "Ā1∩Ā2={(0, 1, 1), (0, 1, 0), (0, 0, 1)}\n"
     ]
    }
   ],
   "source": [
    "#练习1-7\n",
    "from utility import subSet, permutations\n",
    "S=set(permutations([0, 1], 3, repetition=True))\n",
    "A1=subSet(S, lambda a: a[0]==1)\n",
    "A2=subSet(S, lambda a: a[0]==a[1] and a[1]==a[2])\n",
    "A_1=S-A1\n",
    "A_2=S-A2\n",
    "print('A1∪A2=%s'%(A1|A2))\n",
    "print('A1∩A2=%s'%(A1&A2))\n",
    "print('A2-A1=%s'%(A2-A1))\n",
    "print('A1∪Ā2=%s'%(A1|A_2))\n",
    "print('Ā1∩Ā2=%s'%(A_1&A_2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "n=1000, nA=180, fn(A)=0.1800\n",
      "n=10000, nA=1640, fn(A)=0.1640\n",
      "n=100000, nA=16667, fn(A)=0.1667\n",
      "n=1000000, nA=166928, fn(A)=0.1669\n"
     ]
    }
   ],
   "source": [
    "#例1-9\n",
    "import numpy as np\n",
    "for i in range(3, 7):\n",
    "    n=10**i\n",
    "    x=np.random.randint(low=1, high=7, size=n)\n",
    "    hist, _=np.histogram(x, bins=6)\n",
    "    print('n=%d, nA=%d, fn(A)=%.4f'%(n, hist[3], hist[3]/n))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "n=1000, fn(A1)=0.1740, fn(A2)=0.1530, fn(A3)=0.1710, fn(A4)=0.1740, fn(A5)=0.1540, fn(A6)=0.1740\n",
      "n=10000, fn(A1)=0.1709, fn(A2)=0.1675, fn(A3)=0.1622, fn(A4)=0.1686, fn(A5)=0.1602, fn(A6)=0.1706\n",
      "n=100000, fn(A1)=0.1661, fn(A2)=0.1672, fn(A3)=0.1660, fn(A4)=0.1675, fn(A5)=0.1655, fn(A6)=0.1677\n",
      "n=1000000, fn(A1)=0.1663, fn(A2)=0.1665, fn(A3)=0.1665, fn(A4)=0.1663, fn(A5)=0.1676, fn(A6)=0.1668\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYoAAAD4CAYAAADy46FuAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYi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1HgNmq2o6N9sltwPfr6pbuqs7XlBVv59G7eNh3554Tfv2FK2pPYqq+h5Tuv+iqn5dVQ90038CDjK6Y3GoelVVT3azJ3U/g6V0ku3Am4BbhqqxWpK8CLiI0dUbVNVTq/2LtBL79uTYt6dvTQXFaumennge8KOB62xJ8iBwGPhWVQ1Z71PA+4CnB6wxroBvJpnvHtUypJcDR4DPdIcfbklyysA11yX79kRs+r696YMiyanAPcB7q+qPQ9aqqr9V1asZ3S15fpJBDkEkeTNwuKrmh2h/GRdW1WsYPbXyXd2hlqFsBV4DfLqqzgP+DPzD45Q3O/v2xGz6vr2pg6I7nnoPcEdVfWVadbtdye8COwcqcSFweXds9U7gDUk+P1AtAKrqie7Pw8C9jJ5iOZQFYGHsf613M/rlUse+PTn27U0cFN0JuFuBg1X1iSnUOy3JS7rp5wOXAr8YolZVvb+qtlfVDKO7Lu+vqquHqAWQ5JTupCndbvIbgcGu7qmq3wCPJ3ll99IlwCAnatcj+/bk2LdH+jw9dmqSfBF4PbAtyQLwoaq6daByFwJvAx7qjq0CfKCq9g1U73Tg9oy+WOQ5wF1VNfilfVPyMuDe0fjEVuALVfWNgWu+G7ijuyrkUeCdA9c7Ifbtdcu+zRq7PFaStPZs2kNPkqR+DApJUpNBIUlqMigkSU0GhSSpyaCQJDUZFJKkpv8DViXy6WiWzmUAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 4 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "#练习1-8\n",
    "import numpy as np\n",
    "from matplotlib import pyplot as plt\n",
    "for i in range(3, 7):\n",
    "    n=10**i\n",
    "    x=np.random.randint(low=1, high=7, size=n)\n",
    "    hist, bins=np.histogram(x, bins=6)\n",
    "    print('n=%d, fn(A1)=%.4f, fn(A2)=%.4f, fn(A3)=%.4f, fn(A4)=%.4f, fn(A5)=%.4f, fn(A6)=%.4f'\n",
    "          %(n, hist[0]/n, hist[1]/n, hist[2]/n, hist[3]/n, hist[4]/n, hist[5]/n))\n",
    "    plt.subplot(2, 2, i-2)\n",
    "    plt.xticks([1, 2, 3, 4, 5, 6])\n",
    "    ax=plt.gca()\n",
    "    if i%2==0:\n",
    "        ax.yaxis.set_ticks_position('right')\n",
    "    plt.hist(x, bins, density=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "P(A1)=3/8\n",
      "P(A2)=7/8\n"
     ]
    }
   ],
   "source": [
    "#例1-15\n",
    "from utility import subSet, permutations, P\n",
    "S=set(permutations([0, 1], 3, True))\n",
    "A1 = subSet(S, lambda a: sum(a) == 1)\n",
    "p1 = P(A1, S)\n",
    "print('P(A1)=%s' % p1)\n",
    "A2 = subSet(S, lambda a: 1 in a)\n",
    "p2 = P(A2, S)\n",
    "print('P(A2)=%s' % p2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "P(A1)=1/12\n",
      "P(A2)=1/20\n"
     ]
    }
   ],
   "source": [
    "#例1-16\n",
    "from utility import subSet, combinations, P\n",
    "p=[1,2,3,4,5,6,7,8,9,10]\n",
    "S=set(combinations(p, 3))\n",
    "A1=subSet(S, lambda a: min(a)==5)\n",
    "p1=P(A1, S)\n",
    "print('P(A1)=%s'%p1)\n",
    "A2=subSet(S, lambda a: max(a)==5)\n",
    "p2=P(A2, S)\n",
    "print('P(A2)=%s'%p2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "P(A)=1/12\n",
      "P(B)=1/2\n",
      "P(C)=1/2\n",
      "P(D)=1/2\n"
     ]
    }
   ],
   "source": [
    "#练习1-11\n",
    "from sympy.utilities.iterables import variations\n",
    "from utility import subSet, P, permutations\n",
    "p=[1, 2, 3, 4]\n",
    "S=set(permutations(p, len(p)))\n",
    "conditionA=lambda a: ((a[0]==1)&(a[1]==2)&(a[2]==3)&(a[3]==4))|\\\n",
    "                     ((a[0]==4)&(a[1]==3)&(a[2]==2)&(a[3]==1))\n",
    "conditionB=lambda a: (a[0]==1)|(a[3]==1)\n",
    "conditionC=lambda a: ((a.index(1)<3)and(a[a.index(1)+1]==2)or\n",
    "                     (a.index(1)>0)and(a[a.index(1)-1]==2))\n",
    "conditionD=lambda a: a.index(1)>a.index(2)\n",
    "A=subSet(S, conditionA)\n",
    "B=subSet(S, conditionB)\n",
    "C=subSet(S, conditionC)\n",
    "D=subSet(S, conditionD)\n",
    "print('P(A)=%s'%P(A, S))\n",
    "print('P(B)=%s'%P(B, S))\n",
    "print('P(C)=%s'%P(C, S))\n",
    "print('P(D)=%s'%P(D, S))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "P(A)=0.4775\n"
     ]
    }
   ],
   "source": [
    "#例1-17\n",
    "from utility import areaBetween\n",
    "from math import sin, pi\n",
    "a=4\n",
    "l=3\n",
    "zero=lambda x: 0\n",
    "halfa=lambda x: a/2\n",
    "f=lambda x: l*sin(x)/2\n",
    "Aarea=areaBetween(0, pi, zero, f)\n",
    "Sarea=areaBetween(0, pi, zero, halfa)\n",
    "print('P(A)=%.4f'%(Aarea/Sarea))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "P(A)=0.556\n"
     ]
    }
   ],
   "source": [
    "#练习1-12\n",
    "from utility import areaBetween\n",
    "sixty=lambda x: 60\n",
    "zero=lambda x: 0\n",
    "line1=lambda x: x-20\n",
    "line2=lambda x: x+20\n",
    "a1=areaBetween(0, 20, zero, line2)\n",
    "a2=areaBetween(20, 40, line1, line2)\n",
    "a3=areaBetween(40, 60, line1, sixty)\n",
    "Aarea=a1+a2+a3\n",
    "Sarea=areaBetween(0, 60, zero, sixty)\n",
    "print('P(A)=%.3f'%(Aarea/Sarea))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "P(A1∩A2∩Ā3)=1/10\n",
      "P(A1)P(A2|A1)P(Ā3|A1∩A2)=1/10\n",
      "P(A1∩A2∩Ā3)=P(A1)P(A2|A1)P(Ā3|A1∩A2) is True\n"
     ]
    }
   ],
   "source": [
    "#例1-20\n",
    "from utility import subSet, P, condP, permutations\n",
    "S=set(permutations(range(1,11),3))#约定1~9表示合格品，10表示不合格品\n",
    "A1=subSet(S, lambda a: a[0]<=9)#设置事件A1：第1次取到合格品\n",
    "A2=subSet(S, lambda a: a[1]<=9)#A2：第2次取得合格品\n",
    "A_3=subSet(S, lambda a: a[2]>9)#A_3：第3次取得不合格品\n",
    "p1=P(A1&A2&A_3, S)\n",
    "print('P(A1∩A2∩Ā3)=%s'%p1)#输出P(A1∩A2∩Ā3)\n",
    "p2=P(A1, S)*condP(A2, A1)*condP(A_3, A1&A2)\n",
    "print('P(A1)P(A2|A1)P(Ā3|A1∩A2)=%s'%p2)#输出P(A1)P(A2|A1)P(Ā3|A1∩A2\n",
    "print('P(A1∩A2∩Ā3)=P(A1)P(A2|A1)P(Ā3|A1∩A2) is %s'%(p1==p2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "P(B|A)=2/3\n"
     ]
    }
   ],
   "source": [
    "#例1-27\n",
    "from utility import subSet, condP, permutations\n",
    "S=set(permutations(range(1,5),2))\n",
    "A = subSet(S, lambda a: a[0] <= 3)\n",
    "B = subSet(S, lambda a: a[1] <= 3)\n",
    "p = condP(B, A)\n",
    "print('P(B|A)=%s' % p)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "P(A|B)=1/2\n"
     ]
    }
   ],
   "source": [
    "#练习1-19\n",
    "from utility import subSet, condP, permutations\n",
    "S=set(permutations(range(1,6),2))\n",
    "A = subSet(S, lambda a: a[1] >= 4)\n",
    "B = subSet(S, lambda a: a[0] < 4)\n",
    "p = condP(A, B)\n",
    "print('P(A|B)=%s' % p)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "P(A1*A2)=28/45\n",
      "P(A1_*A2_)=1/45\n",
      "P(A1*A2_+A1_*A2)=16/45\n",
      "P(A2_)=1/5\n"
     ]
    }
   ],
   "source": [
    "#例1-28\n",
    "from utility import subSet, condP, P, permutations\n",
    "S=set(permutations(range(1,11),2))\n",
    "A1=subSet(S, lambda a: a[0]<=8)\n",
    "A2=subSet(S, lambda a: a[1]<=8)\n",
    "A1_=S-A1\n",
    "A2_=S-A2\n",
    "p1=P(A1, S)*condP(A2, A1)\n",
    "p2=P(A1_, S)*condP(A2_, A1_)\n",
    "p3=1-p1-p2\n",
    "p4=P(A2_, S)\n",
    "print('P(A1*A2)=%s'%p1)\n",
    "print('P(A1_*A2_)=%s'%p2)\n",
    "print('P(A1*A2_+A1_*A2)=%s'%p3)\n",
    "print('P(A2_)=%s'%p4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "P(B)=23/25\n"
     ]
    }
   ],
   "source": [
    "#例1-32\n",
    "from utility import totalProb, R\n",
    "import numpy as np\n",
    "prioProb=np.array([R(1,2), R(3,10), R(1, 5)])\n",
    "likelihood=np.array([R(9,10), R(14,15), R(19,20)])\n",
    "p=totalProb(prioProb,likelihood)\n",
    "print('P(B)=%s'%p)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "P(B)=1/1000\n"
     ]
    }
   ],
   "source": [
    "#练习1-23\n",
    "from utility import totalProb, R\n",
    "import numpy as np\n",
    "likelihood=np.array([R(1,5), R(4,5)])\n",
    "prioProb=np.array([R(1, 250), R(1,4000)])\n",
    "p=totalProb(prioProb, likelihood)\n",
    "print('P(B)=%s'%p)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "P(C|A)=19/218\n"
     ]
    }
   ],
   "source": [
    "#例1-33\n",
    "from utility import bayes, R\n",
    "import numpy as np\n",
    "prioProb=np.array([R(1,200),R(199,200)])\n",
    "likelihood=np.array([R(19,20), R(1, 20)])\n",
    "p=bayes(prioProb,likelihood, 1)\n",
    "print('P(C|A)=%s'%p)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "P(B|C)=12/13\n"
     ]
    }
   ],
   "source": [
    "#练习1-24\n",
    "from utility import bayes, R\n",
    "import numpy as np\n",
    "likelihood=np.array([R(3,5), R(2,5)])\n",
    "prioProb=np.array([R(4,5), R(1,10)])\n",
    "p=bayes(likelihood, prioProb, 1)\n",
    "print('P(B|C)=%s'%p)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
